<%@ Import namespace="TTQT.TTQT_Code"%>
<%@ Import namespace="System.Data"%>
<%@ Import namespace="System.Data.SqlClient"%>
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="MenuJQuerry.ascx.cs" Inherits="MenuXin_MenuJQuerry" %>

<script runat="server">
    public void Phun(DataRow rTruoc, DataRow r, DataRow rSau)
    {
        //id,Title,parentId,levelParent,kind
        string kq = "";
        int vTruoc=0,v = 0, vSau = 0;
        kq = "<li><a href=\"#\">" + r[2] + "</a>";
        if (rSau != null)
        {
            v = int.Parse(r["levelParent"].ToString());
            vSau = int.Parse(rSau["levelParent"].ToString());
            if (vSau==v)
            {
                kq += "</li>";
            }
            if(vSau>v)
            {
                kq += "<ul>";
            }
            if(vSau<v)
            {
                for(int j=0;j<v-vSau;j++)
                {
                    kq += "</ul></li>";
                }
            }
        }
        if(rSau==null)
        {
            kq += "</li>";
            v = int.Parse(r["levelParent"].ToString());
            for (int j = 0; j < v - 1; j++)
            {
                kq += "</ul></li>";
            } 
        }
        Response.Write(kq);        
    }
    
    public DataTable getAllMenu()
    {
        DataTable result = null;
        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand comm = new SqlCommand("Category_GetForMenu", conn);
        try
        {
            result = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(comm);
            da.Fill(result);
            conn.Close();
            comm.Dispose();
        }
        catch (Exception ee)
        {
            string abc = ee.ToString();
            // Writing log methods here.
        }
        return result;
    }
</script>
    <div id="myslidemenu" class="jqueryslidemenu">
<ul>
<% 
    DataTable d = getAllMenu();
    DataRow r=null, rTruoc=null, rSau=null;
    int soDong = d.Rows.Count;
    for(int i=0;i<soDong;i++)
    {
        if(i==soDong-1)
        {
            rTruoc = d.Rows[i - 1];
            rSau=null;
        }
        if(i==0)
        {
            rSau = d.Rows[i + 1];
        }
        if(i>0 && i<soDong-1)
        {
            rTruoc = d.Rows[i - 1];
            rSau = d.Rows[i + 1];
        }
        r = d.Rows[i];
        if(r==null)
        {
            Response.Write("Shit");
        }
        else Phun(rTruoc, r, rSau);
    }

    /*
    for(int i=0;i<soDong;i++)
    {
        r = d.Rows[i];
        Response.Write("<li><a href=\"#\">" + r["Title"] + "</a>");
    }
     */
%>

</ul>
<div style="clear: left" />
</div>